from strategy.core.Stg import Stg
import talib as ta
import math


class MACDStg(Stg):
    preBuySell = 0
    param = {
        'fastPeriod': 12,
        'slowPeriod': 26,
        'signalPeriod': 9
    }

    def initial(self):
        self.macd = ta.MACD(self.data['close'], self.param['fastPeriod'], self.param['slowPeriod'],
                            self.param['signalPeriod'])
        # 格式: self.macd结果包括: macd,macdsignal,macdhist. 分别对应dif,dea,dif-dea
        # self.fastSma = ta.SMA(self.data, self.param['fastPeriod'])\
        return

    def signal(self, index):
        # 自动跳过无法计算的节点
        if index < self.param['slowPeriod']:
            return
        pre = index - 1
        res = 0
        # dif和dea移动方向向上就卖,向下就卖
        # dif大于dea买, 小于就卖.  金叉买,死叉卖
        # if self.macd['macd'][pre] < self.macd['macd'][index] \
        #         and self.macd['macdsignal'][pre] < self.macd['macdsignal'][index] \
        #         and self.macd['macdhist'][pre] < 0 < self.macd['macdhist'][index]:
        #     res = 1
        # elif self.macd['macd'][pre] > self.macd['macd'][index] \
        #         and self.macd['macdsignal'][pre] > self.macd['macdsignal'][index] \
        #         and self.macd['macdhist'][pre] > 0 > self.macd['macdhist'][index]:
        #     res = -1

        # if self.macd['macd'][pre] < self.macd['macd'][index] and self.macd['macd'][index] > 0:
        #     return 1
        # elif self.macd['macd'][pre] < self.macd['macd'][index] and self.macd['macd'][index] > 0:
        #     return -1

        # macdsignal是dea, macd是dif, macdhist是柱子
        if math.isnan(self.macd['macdsignal'][index]):
            return
        over = self.macd['macdsignal'][index] > 0 and self.macd['macd'][index] > 0
        below = self.macd['macdsignal'][index] < 0 and self.macd['macd'][index] < 0
        up = self.macd['macdsignal'][pre] < self.macd['macdsignal'][index] and \
             self.macd['macd'][pre] < self.macd['macd'][index]
        down = self.macd['macdsignal'][pre] > self.macd['macdsignal'][index] and \
               self.macd['macd'][pre] > self.macd['macd'][index]
        x = self.macd['macdhist'][pre] < 0 < self.macd['macdhist'][index]
        x2 = self.macd['macdhist'][pre] > 0 > self.macd['macdhist'][index]
        if self.param['buy'] == 'overUP':
            if over and up:
                return self.buy()
        if self.param['buy'] == 'belowUp':
            if below and up:
                return self.buy()
        if self.param['buy'] == 'belowUpX':
            if below and up and x:
                return self.buy()
        if self.param['buy'] == 'up':
            if up:
                return self.buy()
        if self.param['sell'] == 'overDown':
            if over and down:
                return self.sell()
        if self.param['sell'] == 'belowDown':
            if below and down:
                return self.sell()
        if self.param['sell'] == 'overDownX2':
            if over and down and x2:
                return self.sell()
        if self.param['sell'] == 'down':
            if down:
                return self.sell()

    def buy(self):  # 在上次买后不再买, 防止重复出现买状态
        if self.preBuySell != 1:
            self.preBuySell = 1
            return 1

    def sell(self):
        if self.preBuySell != -1:
            self.preBuySell = -1
            return -1
# 买点
# 零线上向上运动, 零线下向上运动, 零线下向上运动且dif突破dea(金叉)
# overUP		belowUp		belowUpX
# 卖点
# 零线上向下运动, 零线下向下运动, 零线上向下运动且dif跌破dea(死叉)
# overDown	belowDown		overDownX2
